package com.tencent.wxcloudrun.controller;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.tencent.wxcloudrun.util.PayUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/pay")
@Slf4j
public class PayController {

    @Resource
    private PayUtils payUtils;

    /**
     * 支付回调，待完善业务逻辑，暂时在测试
     *
     * @param payload 支付成功后，微信支付会调用此接口，将支付成功后的数据传过来
     * @return 告知微信支付我们是否成功接受了支付回调信息
     */
    @PostMapping("/paymentNotify")
    public JSONObject handleNotify(@RequestBody String payload) {
        log.info(">>>>微信支付传过来的支付回调数据是   " + payload);
        // todo 处理支付回调逻辑

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("errcode", 0); // 告知微信支付我们已成功接受了
        jsonObject.put("errmsg", "");
        return jsonObject;
    }

    @PostMapping("/unifiedOrder")
    public JSONObject unifiedOrder(HttpServletRequest request) {
        JSONObject jsonObject = payUtils.unifiedOrder(10, IdUtil.getSnowflakeNextIdStr(), request);
        log.info(">>>>将要用于wx.requestPayment方法的数据是  " + JSON.toJSONString(jsonObject));
        return jsonObject;
    }
}
